package com.qf.ayc.vips.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.ayc.income.entity.Income;
import com.qf.ayc.income.service.IIncomeService;
import com.qf.ayc.user.entity.Users;
import com.qf.ayc.utils.ResultVo;
import com.qf.ayc.vips.entity.Vips;
import com.qf.ayc.vips.mapper.VipsMapper;
import com.qf.ayc.vips.service.IVipsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@RestController
@CrossOrigin
@RequestMapping("/AYC/vips")
public class VipsController {
    @Autowired
    private IVipsService vipsService;
    @Autowired
    private VipsMapper vipsMapper;
    @Autowired
    private IIncomeService incomeService;

    //分页的两个接口
    @GetMapping("/getlist")
    private  ResultVo  getlist(Integer pageNum, Integer limit){
        List<Vips> getlist = vipsService.getlist(pageNum, limit);
        QueryWrapper<Vips> wrapper = new QueryWrapper<>();
        //，创建一个分页对象 将pageNum limit要求传入
        Page<Vips> vipsPage = new Page<>(pageNum,limit);
        vipsMapper.selectPage(vipsPage,wrapper);
        //getRecords:得到分页要求的数据
        List<Vips> records = vipsPage.getRecords();

        return ResultVo.ok("200","获取第几页的第几条",records);
    }
    @GetMapping("/total")
    private ResultVo total(){
        long count = vipsService.count();
        System.out.println(count+"vip息总条目数为");
        return ResultVo.ok("200","信息总条目数");
    }

    @PostMapping("/add")
    public ResultVo add(@RequestBody Vips vips) {
        vipsMapper.insert(vips);
        //获取到vip里充值的钱
        Integer recharge = vips.getRecharge();
        //获取当前时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        System.out.println(timestamp);
        Income income = new Income(null,"微信",timestamp,vips.getRecharge());
        //保存数据
        incomeService.save(income);
        return ResultVo.ok("200", "添加成功");
    }

    @GetMapping ("/delete")
    public ResultVo delete(String id) {
        vipsService.delete(id);
        return ResultVo.ok("200", "删除成功");
    }

    @PostMapping("/update")
    public ResultVo update(@RequestBody Vips vips) {
        vipsService.updateById(vips);
        return ResultVo.ok("200", "修改成功");
    }

    @PostMapping("/updatare")
    private  ResultVo updatare(@RequestBody Vips vip){
        //不是置空 是不改数据
//        vip.setId(null);
        vip.setUserid(null);
        vip.setPhone(null);
        vip.setSex(null);
        vip.setPetname(null);


        vipsMapper.updateById(vip);

        return ResultVo.ok("200","vip充值成功成功");
    }


    @PostMapping("/select")
    public ResultVo select(@RequestBody Vips vips) {
        QueryWrapper<Vips> wrapper = new QueryWrapper<>();

//        wrapper.eq(vips.getId() != null, "id", vips.getId())
//                .eq(vips.getUserid() != null, "userid", vips.getUserid());
          wrapper.eq(vips.getPhone() != null, "phone", vips.getPhone());

        List<Vips> data = vipsService.list(wrapper);

        return ResultVo.ok("200", "查询成功", data);

    }

    @GetMapping("/list")
    public ResultVo list() {
        List<Vips> vipslist = vipsService.getlist();
        return ResultVo.ok("200", "所有vip信息", vipslist);
    }




}
